//
//  OnlineLogicViewController.m
//  UniversalApp
//
//  Created by 志刚杨 on 2018/7/26.
//  Copyright © 2018年 voidcat. All rights reserved.
//

#import "OnlineLogicViewController.h"

#import "PersonModel.h"
#import "XYTransitionProtocol.h"
#import "HomeListCollectionViewCell.h"
#import "SDCycleScrollView.h"
#import "RootWebViewController.h"
#import "UserInfoViewController.h"
#import "CallUserViewController.h"
#import "UserPageInfoModel.h"
//cell宽度
#define cellWidth KScreenWidth/2-2
#define SDCycleHeight 140
static NSString *isFirstGu2 = @"isFirstGu2";
static NSString *headerViewIdentifier = @"hederview";
@interface OnlineLogicViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,XYTransitionProtocol,OnlineUserLogic>
@property(nonatomic,strong) OnlineUserLogic *logic;//逻辑层
@property(nonatomic, strong)     UILabel *line2;
@property(nonatomic, strong) UIButton *guideBackground;
@end

@implementation OnlineLogicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //    self.isShowLiftBack = NO;//每个根视图需要设置该属性为NO，否则会出现导航栏异常
    
    //初始化逻辑类
    _logic = [OnlineUserLogic new];
    _logic.page = 1;
    _logic.delegagte = self;
    _logic.type = self.type;
    [_logic loadData];
    [self setupUI];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(getConfigSuccess)
                                                 name:KinitConfigLoginSuccess
                                               object:nil];
    //开始第一次数据拉取
    //    [self.collectionView.mj_header beginRefreshing];
    
}

- (void)getConfigSuccess {
    [[UserManager sharedUserManager] loadUserInfo];
    [_logic loadData];
}

#pragma mark ————— 初始化页面 —————
-(void)setupUI{
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    self.collectionView.collectionViewLayout = layout;
    layout.minimumLineSpacing = 1;//设置最小行间距
    layout.minimumInteritemSpacing = 1;//item间距(最小值)
    layout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);//设置section的编距

    self.collectionView.backgroundColor = CViewBgColor;
    [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([HomeListCollectionViewCell class]) bundle:nil] forCellWithReuseIdentifier:NSStringFromClass([HomeListCollectionViewCell class])];
    
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:headerViewIdentifier];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.view addSubview:self.collectionView];
}


-(void)handleguideEvent
{
    _guideBackground.hidden = YES;
}

#pragma mark ————— 下拉刷新 —————
-(void)headerRereshing{
    _logic.page = 1;
    [_logic loadData];
    [self removeNoDataImage];
    
}

#pragma mark ————— 上拉刷新 —————
-(void)footerRereshing{
    _logic.page+=1;
    [_logic loadData];
    [self removeNoDataImage];
}

#pragma mark ————— 数据拉取完成 渲染页面 —————
-(void)requestDataCompleted{
    [self.collectionView reloadData];
    [self.collectionView.mj_footer endRefreshing];
    [self.collectionView.mj_header endRefreshing];
}

#pragma mark ————— collection代理方法 —————
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _logic.dataArray.count;
}


-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    HomeListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:NSStringFromClass([HomeListCollectionViewCell class]) forIndexPath:indexPath];
    if(_logic.dataArray.count == 0)
    {
        
    }
    else
    {
        cell.personModel = _logic.dataArray[indexPath.item];
    }
    
    return cell;
}

//*******重写的时候需要走一句话
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(_logic.dataArray.count == 0)
    {
        return;
    }
    UserListInfo *model = _logic.dataArray[indexPath.item];
    UserInfoViewController *vc = [[UserInfoViewController alloc] init];
    vc.uid = model.id;
    [self.navigationController pushViewController:vc animated:YES];
    return;
    //        //标记cell
    //        [self.collectionView setCurrentIndexPath:indexPath];
    //
    //        PersonListCollectionViewCell *cell =(PersonListCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
    //
    //        ProfileViewController *profileVC = [ProfileViewController new];
    //        profileVC.headerImage = cell.imgView.image;
    //        profileVC.isTransition = YES;
    //        [self.navigationController pushViewController:profileVC animated:YES];
    
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    if (section == 0 )
    {
        return UIEdgeInsetsMake(0, 0, 0, 0);
    }
    
    return UIEdgeInsetsMake(0, 0, 0, 0);
}


- (CGSize)collectionView:(nonnull UICollectionView *)collectionView layout:(nonnull UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(nonnull NSIndexPath *)indexPath{
    return CGSizeMake(cellWidth,cellWidth);
}


#pragma mark ————— 转场动画起始View —————
//-(UIView *)targetTransitionView{
//    NSIndexPath * indexPath = [self.collectionView currentIndexPath];
//    PersonListCollectionViewCell *cell =(PersonListCollectionViewCell *)[self.collectionView cellForItemAtIndexPath:indexPath];
//    return cell.imgView;
//}

-(BOOL)isNeedTransition{
    return YES;
}


-(void)naviBtnClick:(UIButton *)btn{
    DLog(ASLocalizedString(@"点击了筛选按钮"));
}

-(void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
    self.collectionView.frame = CGRectMake(0, 0, KScreenWidth, SCREEN_HEIGHT - TabBarHeight - kTopHeight);
    
}

#pragma mark -  上下滑动隐藏/显示导航栏

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
